#include<oslib/oslib.h>
#include<string.h>
#include"../function.h"


PSP_MODULE_INFO("Timer Test", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(6*1024);

#define WHITE 0xFFFFFFFF
#define BLACK 0x00000000

int callback(int arg1, int arg2, void *common)
{
      oslEndGfx();
      oslQuit();
      return 0;
}

main(){
    /* Init OSLib */
    oslInit(0);
    oslInitGfx(OSL_PF_8888, 1);
    oslSetQuitOnLoadFailure(1);
    oslInitConsole();    

    /* Setup OSLib Exit Callback */
    oslSetExitCallback(callback);
  
    Timer *timer = Timer_new();
    unsigned long time;
    char * status;

    while(1){
        oslStartDrawing();
        oslClearScreen(BLACK);
        
        oslReadKeys();
        
        time = Timer_get(timer);
        status = Timer_status(timer);

        if(osl_pad.pressed.square)
            Timer_stop(timer);
        if(osl_pad.pressed.circle){
            if(!strcmp(status,"pause"))
                Timer_resume(timer);
            else if(!strcmp(status,"stop"))
                Timer_start(timer);
            else
                Timer_pause(timer);
        }
        if(osl_pad.pressed.triangle)
            Timer_reset(timer,100);
        if(osl_pad.pressed.cross)
            break;


        oslPrintf_xy(1,1,"time:%dl",time);
        oslPrintf_xy(1,20,"status:%s",status);
        oslPrintf_xy(1,50,"O:start/resume/pause /\:resets timer to 100 []:stop X:exit");
        
        /* End Drawing And Frame */
        oslEndDrawing();
        oslEndFrame();
        oslSyncFrame();
    }

    /* End Graphics And Quit */
    oslEndGfx();
    oslQuit();    
}